import { createApp } from "vue"
import "./style.css"
import App from "./App.vue"
import router from "./router"
import * as ElementPlusIconsVue from "@element-plus/icons-vue"
import store from "./store"
import panelHead from "./components/panelHead.vue"

// 刷新后的动态路由添加
const loacalData = localStorage.getItem("pz_v3")
if (loacalData) {
  store.commit("dynamicMenu", JSON.parse(loacalData).menu.routerList)
  store.state.menu.routerList.forEach((item) => {
    router.addRoute("main", item)
  })
}

router.beforeEach((to, from) => {
  const token = localStorage.getItem("pz_token")
  // 非登录页面token不存在
  if (!token && to.path !== "/login") {
    return "./login"
  } else if (token && to.path === "/login") {
    return "/"
  } else {
    // 登录页面token存在
    return true
  }
})

const app = createApp(App)
// 路由挂载
app.use(router)
// store挂载
app.use(store)
app.mount("#app")
app.component("panelHead", panelHead)
// 图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
